skip to Main Content

I first convert txt to JSON.

const txt = JSON.stringify(`{
  ErrorList: [{ 80: 'Prepared' }],
  Reference: [
    {
      'Rule Name': 'Missing 3',
      'Rule ID': 1,
      'Rule Des': 'Did they provide News ?'
    },
    {
      'Rule Name': 'Missing Zip',
      'Rule ID': 2,
      'Rule Des': 'Is field ?'
    }
  ],
  'Hard Violation': [
    {
      Violation: 0,
      'Initial Servicer': 'BI',
      'Total Confirmed': 0,
      'Last Date': '05/16/2021'
    },
    {
      Violation: 0,
      'Initial Servicer': 'BI',
      'Total Confirmed': 3,
      'Last Date': '12/19/2019'
    }
  ]
}`);

const obj = JSON.parse(txt);
console.log(obj.Reference);

Then ,I tried to get some value of the JSON ,I tried to print the "Reference" inside it ,but it returns "undefined" ,any friend can help ?

2

Answers


  1. Chosen as BEST ANSWER

    Thank you for all your reply ,especially from @ T.J. Crowder

    The best way for me is create an object, do it directly:

    const obj = { ErrorList: [ { 80: "Prepared", /*...*/ } ] /*...*/ }; 
    

  2. you have to do like this
    
    const text = `{
    
        "ErrorList": [
            {
                "80": "Prepared"
            }
        ],
        "Reference": [
            {
                "Rule Name": "Missing 3",
                "Rule ID": 1,
                "Rule Des": "Did they provide News ?"
            },
            {
                "Rule Name": "Missing Zip",
                "Rule ID": 2,
                "Rule Des": "Is field ?"
            }
        ],
        "Hard Violation": [
            {
                "Violation": 0,
                "Initial Servicer": "BI",
                "Total Confirmed": 0,
                "Last Date": "05/16/2021"
            },
            {
                "Violation": 0,
                "Initial Servicer": "BI",
                "Total Confirmed": 3,
                "Last Date": "12/19/2019"
            }
        ]
    }`;
    
    
    // Parse the JSON string into a JavaScript object
    const jsonObj = JSON.parse(text);
    
    
    // Access and manipulate the object as needed
    
    console.log(jsonO`enter code here`bj.ErrorList);
    console.log(jsonObj.Reference);
    console.log(jsonObj['Hard Violation']);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search